package com.school.vaccination.controller;

import com.github.pagehelper.PageInfo;
import com.school.support.base.BaseController;
import com.school.vaccination.domain.Child;
import com.school.vaccination.domain.Vaccine;
import com.school.vaccination.model.ChildWaitModel;
import com.school.vaccination.model.RecordModel;
import com.school.vaccination.service.ChildService;
import com.school.vaccination.service.RecordService;
import com.school.vaccination.service.VaccineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.common.support.base.Page;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleType;
import zhibi.common.support.mybatis.example.ExampleUtil;

/**
 * Created by 执笔 on 2017/3/18.
 */
@Controller
@RequestMapping("record")
public class RecordController extends BaseController {

    @Autowired
    private RecordService recordService;
    @Autowired
    private ChildService childService;
    @Autowired
    private VaccineService vaccineService;

    /**
     * 接种记录
     * @param model
     * @param page
     * @param recordModel
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Page page, RecordModel recordModel) {
        Example example = ExampleUtil.getInstance()
                .addParam("c.name", recordModel.getChildName(), ExampleType.Operation.LIKE)
                .addParam("v.name", recordModel.getVaccineName(), ExampleType.Operation.LIKE)
                .addParam("v.number", recordModel.getVaccineNumber(), ExampleType.Operation.LIKE)
                .addOrder("r.addtime", ExampleType.OrderType.DESC)
                .getExample();
        PageInfo<RecordModel> pageInfo = recordService.selectModel(example, page);
        setModelAttribute(model, pageInfo);
        return "record/list";
    }

    /**
     * 待接种名单
     * @return
     */
    @RequestMapping("wait")
    public String waitList(Page page, Model model, ChildWaitModel childWaitModel) {
        Example example = ExampleUtil.getInstance()
                .addParam("t.name", childWaitModel.getName(), ExampleType.Operation.LIKE)
                .addParam("t.vaccinename", childWaitModel.getVaccineName(), ExampleType.Operation.LIKE)
                .addParam("t.vaccinename", childWaitModel.getVaccineNumber(), ExampleType.Operation.LIKE)
                .addParam("t.recordcount < t.vaccinecountmax")
                .getExample();
        PageInfo<ChildWaitModel> pageInfo = recordService.selectWaitModel(example, page);
        setModelAttribute(model, pageInfo);
        return "record/wait-list";
    }

    @RequestMapping("invince")
    public String invince(Integer childId,Integer vaccineId){
        Child child = childService.selectByPK(childId);
        if(null == child){
            throw new MessageException("该小孩不存在");
        }
        Vaccine vaccine = vaccineService.selectByPK(vaccineId);
        if (null == vaccine || vaccine.getStatus() != STATE_1) {
            throw new MessageException("该疫苗不存在或已过期");
        }
        recordService.invince(child,vaccine);
        return redirect("wait");
    }
}
